package com.bix.util.blizfiles.m2;

import java.nio.ByteBuffer;

import org.apache.log4j.Logger;

import com.bix.util.blizfiles.BufferUtils;
import com.jme.math.Vector2f;
import com.jme.math.Vector3f;

/**
 * This class represents a vertex that is loaded from a M2 file.
 * 
 * @author squid
 * 
 * @version 1.0.0
 */
public class M2Vertex {
	private static final Logger log = Logger.getLogger (M2Vertex.class.getName ());

	private int numBones;
	private Vector3f position;
	private float[] boneWeights;
	private byte[] boneIndices;
	private Vector3f normalVector;
	private Vector2f textureCoordinate;
	private Vector2f unknown;

	public M2Vertex() {
		this.numBones = 4;
	}

	public M2Vertex(ByteBuffer bb) {
		this.numBones = 4;
		read (bb);
	}

	/**
	 * Read a vertex object from a ByteBuffer.
	 * 
	 * @param bb	The buffer to read from.
	 */
	public void read (ByteBuffer bb) {
		this.position = BufferUtils.getVector3f (bb);

		//
		// The bone weights are values that must be divided by 255 to get the actual
		// weight as they come in as bytes and need to be floats. The weights added
		// together are "supposed" to add up to 1.0.  There is one weight per bone.
		//
		this.boneWeights = new float[this.numBones];
		for (int i = 0; i < this.numBones; i++) {
			byte	b = bb.get ();
			boneWeights[i] = ((float) ((int)b & 0xFF)) / 255.0f;
//			log.debug ("  b[" + b + "]=[" + boneWeights[i] + "]");
		}

		this.boneIndices = BufferUtils.getByteArray (bb, this.numBones);
		this.normalVector = BufferUtils.getVector3f (bb);
		this.textureCoordinate = BufferUtils.getVector2f (bb);
		this.unknown = BufferUtils.getVector2f (bb);
		
		log.debug ("  Position[" + this.getPosition ().getX () + "," + this.getPosition ().getY () + ","
				+ this.getPosition ().getZ () + "]");
		for (int i = 0; i < this.numBones; i++) {
			log.debug ("  BoneIndex [" + i + "][" + this.getBoneIndices ()[i] + "]");
			log.debug ("  BoneWeight[" + i + "][" + this.getBoneWeights ()[i] + "]");
		}
		log.debug ("  Normal[" + this.getNormalVector ().getX () + "," + this.getNormalVector ().getY () + ","
				+ this.getNormalVector ().getZ () + "]");
		log.debug ("  TextureCoordinate[" + this.getTextureCoordinate ().getX () + ","
				+ this.getTextureCoordinate ().getY () + "]");
		log.debug ("  Unknown[" + this.getUnknown ().getX () + "," + this.getUnknown ().getY () + "]");
	}

	/**
	 * @return the position
	 */
	public Vector3f getPosition () {
		return position;
	}

	/**
	 * Returns the weight of the bones attached to this vertex.
	 * 
	 * @return The bone weights for the bones.
	 */
	public float[] getBoneWeights () {
		return boneWeights;
	}

	/**
	 * @return the boneIndices
	 */
	public byte[] getBoneIndices () {
		return boneIndices;
	}

	/**
	 * @return the normalVector
	 */
	public Vector3f getNormalVector () {
		return normalVector;
	}

	/**
	 * @return the textureCoordinates
	 */
	public Vector2f getTextureCoordinate () {
		return textureCoordinate;
	}

	/**
	 * @return the unknown
	 */
	public Vector2f getUnknown () {
		return unknown;
	}
}
